/*
 ============================================================================
 Name        : MulImage.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CMulImage implementation
 ============================================================================
 */
#include <EIKENV.H>
#include <BAUTILS.H>
#include <UTF.h>
#include <eikapp.h>

#include "MulImage.h"
#include "fbutils.h"
#include "ConstDefined.hrh"

CMulImage::CMulImage (MMulImageObserver* observer ) :
	m_pImageUtility (NULL),
	m_flagConvert (EFalse ), m_pObserver (observer )
{
	// No implementation required
}

CMulImage::~CMulImage ()
{
	GetAppUi ()->CancelHttpQueryL (m_querySession, ETrue );
	if (m_pImageUtility )
	{
		//m_pImageUtility->CancelConvertL();
		delete m_pImageUtility;
	}
	if (m_pBitmap )
		delete m_pBitmap;
	
	if (m_pBitmapMask )
		delete m_pBitmapMask;
}
/**
 * ͨ������ת��ͼƬ
 */
void CMulImage::OpenDataL (const TDesC8 &aFileName, TBool aNeedMasked, TMdaClipFormat *aFormat, TMdaPackage *aCodec, TMdaPackage *aExtra )
{
	HBufC16 *pUri = CnvUtfConverter::ConvertToUnicodeFromUtf8L (aFileName );
	OpenFileL (pUri->Des (), aNeedMasked, aFormat, aCodec, aExtra );
	delete pUri;
}

/**
 * ͨ���ļ�·������url��ת��ͼƬ
 */
void CMulImage::OpenFileL (const TDesC &aFileName, TBool aNeedMasked, TMdaClipFormat */*aFormat*/, TMdaPackage */*aCodec*/, TMdaPackage */*aExtra*/ )
{
	m_flagMask = aNeedMasked;
	
	if (aFileName.FindF (_L("http://")) == 0 )
	{
		//����ͼƬ�ļ���
		/*
		 m_cacheFile
		 = CEikonEnv::Static ()->EikAppUi ()->Application ()->AppFullName ();
		 #ifdef __WINS__
		 TBuf<2> drive(_L("c:"));
		 #else
		 TBuf<2> drive (m_cacheFile.Left (2 ) );
		 #endif
		 */
		m_cacheFile.Copy (KFbContentsPath );
		AddDrive(m_cacheFile);
		m_cacheFile.Append (_L("\\icons\\"));
		TInt pos;
		TPtrC ptr (aFileName );
		for (pos = ptr.Length () - 1; pos >= 0; pos-- )
		{
			if (ptr[pos] == '/' )
				break;
		}
		//ȷ��·������
		BaflUtils::EnsurePathExistsL (CEikonEnv::Static ()->FsSession (),
				m_cacheFile );

		//�ж��ļ��Ƿ���ڻ�����
		m_cacheFile.Append (ptr.Right (ptr.Length () - 1 - pos ) );
		if (BaflUtils::FileExists (CEikonEnv::Static ()->FsSession (),
				m_cacheFile ) )
		{
			if (!m_pImageUtility )
				m_pImageUtility = CMdaImageFileToBitmapUtility::NewL (*this );
			((CMdaImageFileToBitmapUtility *) m_pImageUtility)->OpenL (
					m_cacheFile );
		}
		else
		{
			//�������ȡͼƬ
			HBufC8 *pUri = CnvUtfConverter::ConvertFromUnicodeToUtf8L (
					aFileName );
			m_querySession = (TInt) pUri;
			GetAppUi ()->IssueHttpGetL (this, pUri->Des (), m_querySession );
			delete pUri;
		}
		
	}
	else
	{
		//���ص�ַ
		//����CMdaImageFileToBitmapUtility�������
		if (!m_pImageUtility )
			m_pImageUtility = CMdaImageFileToBitmapUtility::NewL (*this );
		((CMdaImageFileToBitmapUtility *) m_pImageUtility)->OpenL (aFileName );
	}
	
}

/**
 * ��ȡBitmap������
 * ���ת��δ��ɷ���ΪNULL��
 */
CFbsBitmap* CMulImage::GetBitmap ()
{
	if (m_flagConvert )
		return m_pBitmap;
	else
		return NULL;
}

/**
 * ��ȡMasked Bitmap������
 * ���ת��δ��ɷ���ΪNULL,�����ʹ��ʱ�����ָ���ж���α
 */
CFbsBitmap* CMulImage::GetBitmapMasked ()
{
	if (m_flagConvert )
		return m_pBitmapMask;
	else
		return NULL;
}

//������ MMdaImageUtilObserver:
void CMulImage::MiuoOpenComplete (TInt aError )
{
	if (aError == KErrNone )
	{
		TFrameInfo frameInfo;
		
		m_pImageUtility->FrameInfo (0, frameInfo );
		m_pBitmap = new (ELeave) CFbsBitmap ();
		m_pBitmap->Create (frameInfo.iOverallSizeInPixels, EColor64K );
		if (m_flagMask )
		{
			m_pBitmapMask = new (ELeave) CFbsBitmap ();
			m_pBitmapMask->Create (frameInfo.iOverallSizeInPixels, EGray2 );
			m_pImageUtility->ConvertL (*m_pBitmap, *m_pBitmapMask );
			
		}
		else
		{
			m_pImageUtility->ConvertL (*m_pBitmap );
		}
		
	}
}

//������ MMdaImageUtilObserver:
void CMulImage::MiuoCreateComplete (TInt /*aError*/ )
{
	m_flagConvert = EFalse;
}

//������ MMdaImageUtilObserver:
void CMulImage::MiuoConvertComplete (TInt aError )
{
	if (aError == KErrNone )
	{
		m_flagConvert = ETrue;
		if (m_pObserver )
			m_pObserver->MulImageReady (this );
	}
}

/**
 * WebClientEvent()
 * Called when event occurs in CWebClientEngine.
 * @param aEventDescription: A event in textual format, e.g.
 *                           "Transaction Successful"
 */
void CMulImage::WebClientEvent (const TInt /*aEvent*/, TInt /*aSessionId*/, TInt /*aCode*/)
{
	
}

/**
 * WebClientHeaderReceived()
 * Called when HTTP header is received.
 * @param aHeaderData: Header field name and value
 */
void CMulImage::WebClientHeaderReceived (const TDesC8& /*aHeaderFiled*/, const TDesC8& /*aHeaderVal*/, TInt /*aSessionId*/)
{
	
}

/**
 * WebClientBodyReceivedComplete()
 * Called when a part of the HTTP body is received.
 * @param aBodyData:  Part of the body data received. (e.g. part of
 *                    the received HTML page)
 */
void CMulImage::WebClientBodyReceivedComplete (const TDesC8& aBodyData, TInt aSessionId )
{
	if (aSessionId != m_querySession )
		return;

	//����ͼƬ�����ػ�����
	RFile file;
	//open file for write
	TInt result = file.Create (CEikonEnv::Static ()->FsSession (), m_cacheFile,
			EFileWrite );
	if (result == KErrNone )
	{
		result = file.Write (aBodyData );
		if (result == KErrNone )
			result = file.Flush ();
		file.Close ();
	}
	//����ͼƬ
	if (BaflUtils::FileExists (CEikonEnv::Static ()->FsSession (), m_cacheFile ) )
	{
		if (!m_pImageUtility )
			m_pImageUtility = CMdaImageFileToBitmapUtility::NewL (*this );
		((CMdaImageFileToBitmapUtility *) m_pImageUtility)->OpenL (m_cacheFile );
	}
}

void CMulImage::ClientBodyReceived (const TDesC8& /*aBodyData*/, TBool /*aIsLast*/)
{
	
}

